(function(w){
    var opt = {
        getDoms : function(){
            this.doms = {
                time : $('.time'),
                MyPai : $('.MyPai'),
                leftPais : $('.leftPais'),
                RightPais : $('.RightPais'),
                mplay : $('.mplay'),
                MyPais : $('.MyPais'),
                audio : $('#audio'),
                mydata : $('#mydata'),
                leftdata : $('#leftdata'),
                rightdata : $('#rightdata'),
                dataId : $('.dataId'),
                blockpeng : $('#blockpeng'),
                mypengPai : $('.mypengPai'),
                blockgang : $('#blockgang'),
                blockliang :$('#blockliang'),
                width : 2,
                clickOne : true,
                xianzhichupai : true,
                liangNumber : 1,
                dianjizhezao : $('.dianjizhezao'),
                xiaohubunengliang : 0,
                zimo : 0
            }
        },
        init : function(){
            var _self = this;
            _self.getTitleDate();
            _self.getDoms();
            _self.addEvent();
            _self.time();
            var urlData = w.location.href;
            var rid = urlData.split('/')[urlData.split('/').length-4];
            var mid = urlData.split('/')[urlData.split('/').length-3];
            var pasId = urlData.split('/')[urlData.split('/').length-5];
            urlData = ['tels',urlData.split('/')[urlData.split('/').length-5]];
            var roles = {
                host : ['r2','host','r1'],
                r1 : ['host','r1','r2'],
                r2 : ['r1','r2','host']
            };
            urlData =  roles[urlData[1]];
//            var k = _self.getHoom();//清空本地的牌
//            _self.clearStorage(k);清空本地的牌
//            var wsServer ='ws://192.168.0.199:9502';  //地址
            var wsServer ='ws://139.224.68.110:9502';  //地址
            ws = new WebSocket(wsServer); //监听链接
            ws.onopen = function (evt) {  //测试链接成功或失败
                //websocket.readyState 属性：
                switch (ws.readyState) {
                    case 0: console.log('通信服务未打开');break;
                    case 1: console.log('连接成功');break;
                    case 2: console.log('通信端口关闭，无法连接');break;
                    case 3: console.log('通信服务已关闭');break;
                }
                ws.send(JSON.stringify({
                    rid : rid,
                    role : pasId,
                    mid : mid,
                    type : 3
                }));
            };

            var cookiedata = _self.getStorage(_self);  //获取cookie里面是否有值
            ws.onmessage = function (evt) { //返回数据
                var evtData = JSON.parse(evt.data);
                if(evtData.type==3){

                    if(evtData.cards){
                        $('.MyPais').html('');
                        if(evtData.next ==urlData[1]){
                            $('#mydata').css('display','block')
                        }
                        if(evtData.next ==urlData[0]){
                            $('#leftdata').css('display','block')
                        }
                        if(evtData.next ==urlData[2]){
                            $('#rightdata').css('display','block')
                        }
                        console.log(evtData.cards[urlData[1]].cards.hand.length)
                        if(evtData.cards[urlData[1]].cards.hand.length<=11){
                            $('.MyPai').css('margin-left','-25%')
                        }
                        if(evtData.cards[urlData[1]].cards.hand.length<=8){
                            $('.MyPai').css('margin-left','-20%')
                        }

                        _self.diaoxiangang(evtData.cards[urlData[1]].cards.gang)
                        _self.diaoxianpengt(evtData.cards[urlData[1]].cards.peng)
                        _self.dachuqudepai(evtData.cards[urlData[1]].cards.send);
                        _self.diaoxianshoupai(evtData.cards[urlData[1]].cards.hand);
                        _self.diaoxianleft(evtData.cards[urlData[0]].cards.hand);
                        _self.diaoxianRight(evtData.cards[urlData[2]].cards.hand);
                        _self.leftpengt(evtData.cards[urlData[0]].cards.peng);
                        _self.leftgang(evtData.cards[urlData[0]].cards.gang);
                        _self.rightpeng(evtData.cards[urlData[2]].cards.peng);
                        _self.rightgang(evtData.cards[urlData[2]].cards.gang);

                    }else {

                        _self.doms.dianjizhezao.css('display', 'none');
                        $('#over').css('display', 'none');
                        $('.leftoverpais').html('');
                        $('.rightoverpais').html('');
                        $('.mypengPai').html('');
                        $('.mplay').html('');
                        $('#blockHupai').css('display', 'none');
                        $('.MyPai').css('marginLeft', '-39%');
                        $('#blockHupai').css('display', 'none');
                        $('#blockpeng').css('display', 'none');
                        $('#blockgang').css('display', 'none');
                        $('#blockliang').css('display', 'none');
                    }
                    _self.doms.width = 2;

                    if(evtData.playlist!=undefined){
                        var key = _self.getHoom();
                        _self.displayCp(urlData, evt.data,_self);
                        var parseJson = JSON.parse(evt.data);
                        var name = new Array();
                        name.push(
                            {
                                jsonList: []
                            },
                            {
                                payrest: []
                            },
                            {
                                data: []
                            }
                        );
                        name[1].payrest.push(parseJson.playlist.payrest);
                        name[2].data.push(parseJson);
                        _self.getcookie(name, key);
                        _self.templateMy(_self, evt.data, urlData[1]);
                        _self.templateLeft(_self, evt.data, urlData[0]);
                        _self.templateRight(_self, evt.data, urlData[2]);
                    };
                }
                if(evtData.type==5){
                    _self.doms.xianzhichupai = true;
                    if(evtData.cid) {
                        if (urlData[1] == evtData.next) {
                            _self.overpais($('.leftoverpais'), evtData.cid, 'left');
                        }
                        if (urlData[1] != evtData.next) {
                            _self.overpais($('.rightoverpais'), evtData.cid, 'right');
                        }
                    }
                    var roles = {
                        host: ['r2', 'host', 'r1'],
                        r1: ['host', 'r1', 'r2'],
                        r2: ['r1', 'r2', 'host']
                    };
                    var vals = _self.getStorage(_self);
                    /*status = 0 什么都不是
                     = 1 胡牌
                     = 2 杠
                     = 3 碰
                     * */
                    if(evtData.res==0) {
                        //默认操作
                        _self.displayCp(roles[urlData[1]], evtData, _self, true); //
                        var val = vals[2].data[0].playlist.payrest[0];
                        vals[2].data[0].playlist.payrest.splice(0, 1);
                        if(evtData.next==urlData[1]) {
                            var key = _self.getHoom();
                            _self.getcookie(vals, key);
                            var options = {
                                type: 6,
                                rid: rid,
                                id : val.id,
                                role : urlData[1]
                            };
                            options = JSON.stringify(options);
                            ws.send(options);
                            //摸牌的逻辑
                            $('#shengyupai').html(vals[2].data[0].playlist.payrest.length);
                            _self.Hupai(val, vals[2].data[0].playlist, urlData, evtData.next,_self);
                            _self.templateMyOne(_self, val);
                        }
                    }else
                    if(evtData.res==1) {
                        //有人胡牌
                    }else
                    if(evtData.res==2) {
                        if(urlData[1]==evtData.pre){
                            $('.Myplay li:last-child').remove();
                        }
                        if(urlData[2]==evtData.pre){
                            $('.rightoverpais li:last-child').remove();
                        }
                        if(urlData[0]==evtData.pre){
                            $('.leftoverpais li:last-child').remove();
                        }
                        //有人杠
                        if(evtData.cid) {
                            if (urlData[0]==evtData.next) {
                                _self.templateLeft(_self,evtData,urlData,true,'gang');
                            }else if (urlData[2] == evtData.next) {
                                _self.templateRight(_self,evtData,urlData,true,'gang');
                            }
                        }
                        var val = vals[2].data[0].playlist.payrest[0];
                        vals[2].data[0].playlist.payrest.splice(0, 1);
                        if(evtData.next==urlData[1]) {
                            var key = _self.getHoom();
                            _self.getcookie(vals, key);
                            var options = {
                                type: 6,
                                rid: rid,
                                id : val.id,
                                role : urlData[1]
                            };
                            options = JSON.stringify(options);
                            ws.send(options);
                            //摸牌的逻辑
                            _self.Hupai(val, vals[2].data[0].playlist, urlData, evtData.next,_self);
                            _self.templateMyOne(_self, val);
                        }
                        _self.displayCp(roles[urlData[1]], evtData, _self, true); //
                    }else
                    if(evtData.res==3) {

                        if(urlData[1]==evtData.pre){
                            $('.Myplay li:last-child').remove();
                        }
                        if(urlData[2]==evtData.pre){
                            $('.rightoverpais li:last-child').remove();
                        }
                        if(urlData[0]==evtData.pre){
                            $('.leftoverpais li:last-child').remove();
                        }
                        //有人碰
                        if(evtData.cid) {
                            if (urlData[0]==evtData.next) {
                                _self.templateLeft(_self,evtData,urlData,true);
                            }else if (urlData[2] == evtData.next) {
                                _self.templateRight(_self,evtData,urlData,true);
                            }
                        }
                        _self.displayCp(roles[urlData[1]], evtData, _self, true); //

                    }else{
                        _self.Hupais(evtData,vals[2].data[0].playlist[urlData[1]],_self);
                        _self.peng(evtData,vals[2].data[0].playlist[urlData[1]],urlData[1],mid,roles,rid,_self,vals,urlData);
                    }
                }
                if(evtData.type==0){


                }
                if(evtData.type==6){
                    var key = _self.getHoom()
                    var val = _self.getStorage(_self);
                    val[2].data[0].playlist.payrest.splice(0,1);
                    _self.getcookie(val,key);
                }
                if(evtData.type==8){

                    var test1 ='';
                    var test2 = '';
                    var test3 = ''
                    for(var i=0;i<evtData.players.host.length;i++){
                        test1+='<img src="../imgs/'+evtData.players.host[i].type+'-'+evtData.players.host[i].cate+'.png">'
                    }
                    for(var i=0;i<evtData.players.r1.length;i++){
                        test2+='<img src="../imgs/'+evtData.players.r1[i].type+'-'+evtData.players.r1[i].cate+'.png">'
                    }
                    for(var i=0;i<evtData.players.r2.length;i++){
                        test3+='<img src="../imgs/'+evtData.players.r2[i].type+'-'+evtData.players.r2[i].cate+'.png">'
                    }
                   $('.oneImg').append(test1);
                    $('.toImg').append(test2);
                    $('.sImg').append(test3);
                    $('.startGame').attr('src','../imgs/redey.png');
                    $('#over').css('display','block');
                    $('.startGame').on('click',function(){
                        $('.startGame').attr('src','../imgs/readey1.png');
                    })
                }
                if(evtData.type==9){
                    var roleName = _self.getHoom();
                    var this_liang = evtData.role;
                    var val = _self.setcookie(roleName);
                }
            }
        },
        rightgang : function(data){
            var text = ''
            for(var i=0;i<data.length;i++){
                text+='<li class="RightPai dao">' +
                    '<img src="../imgs/'+data[i].type+"-"+data[i].cate+'_right_gang.png" alt=""/>' +
                    '</li>'
            }
            $('.RightPais').append(text);
        },
        rightpeng : function(data){
            var text = ''
            for(var i=0;i<data.length;i++){
                text+='<li class="RightPai dao">' +
                    '<img src="../imgs/'+data[i].type+"-"+data[i].cate+'_right_peng.png" alt=""/>' +
                    '</li>'
            }
            $('.RightPais').append(text);
        },
        leftgang : function(data){
            var text = ''
            for(var i=0;i<data.length;i++){
                text+='<li class="leftPai dao">' +
                    '<img src="../imgs/'+data[i].type+"-"+data[i].cate+'_left_gang.png" alt=""/>' +
                    '</li>'
            }
            $('.leftPais').append(text);
        },
        leftpengt : function(data){
            var text = ''
            for(var i=0;i<data.length;i++){
                text+='<li class="leftPai dao">' +
                    '<img src="../imgs/'+data[i].type+"-"+data[i].cate+'_left_peng.png" alt=""/>' +
                    '</li>'
            }
            $('.leftPais').append(text);
        },
        diaoxiangang : function(data){
            $('.mypengPai').css('display','block');
            $('.mypengPai').html('');
            var text = ''
            for(var i=0;i<data.length;i++){
                text+='<li >' +
                    '<img src="../imgs/'+data[i].type+"-"+data[i].cate+'_center_gang.png" alt=""/>' +
                    '</li>'
            }
            $('.mypengPai').append(text);
        },
        diaoxianpengt : function(data){
            $('.mypengPai').css('display','block')
            $('.mypengPai').html('');
            var text = ''
            for(var i=0;i<data.length;i++){
                text+='<li >' +
                    '<img src="../imgs/'+data[i].type+"-"+data[i].cate+'_center_peng.png" alt=""/>' +
                    '</li>'
            }
            $('.mypengPai').append(text);
        },
        diaoxianRight : function(data){
            $('.RightPais').html('');
            var text = ''
            for(var i=0;i<data.length;i++){
                text+='<li class="RightPai">' +
                    '<img src="../imgs/pai_left.png" alt=""/>' +
                    '</li>'
            }
            $('.RightPais').append(text);
        },
        diaoxianleft : function(data){
            $('.leftPais').html('');
            var text = ''
            for(var i=0;i<data.length;i++){
                text+='<li class="leftPai">' +
                '<img src="../imgs/pai_left.png" alt=""/>' +
                '</li>'
            }
            $('.leftPais').append(text);
        },
        dachuqudepai : function(data){
            $('.mplay').html('');
            var text = ''
            for(var i=0;i<data.length;i++){
                text+="<li>" +
                    "<img src='../imgs/"+data[i].type+"-"+data[i].cate+"_center.png' alt=''>" +
                "</li>"
            }
            $('.mplay').append(text);

        },
        diaoxianshoupai: function(data){
            $('.MyPai').html('');
            var text = ''
            for(var i=0;i<data.length;i++){
                text+="<li dataindex="+data[i].id+" data='"+data[i].type+"-"+data[i].cate+"'>" +
                "<img src='../imgs/"+data[i].type+"-"+data[i].cate+".png' alt=''>" +
                "</li>"
            }
            $('.MyPai').append(text);
        },
        resetData : function(role,pai){
            var roleName = _self.getHoom();
            var val = _self.setcookie(roleName);

        },
        getTitleDate : function(){
            var jonin_info = window.location.href.split('?')[1];
            var info = jonin_info.split('/');
            var joinInfo = {
                'role': info[1],
                'rid': info[2],
                'mid': info[3],
                'token' : info[4]
            };
            var opt = {
                url : 'http://mj.lcc.so/game/num/'+joinInfo.role+'/'+joinInfo.rid+'/'+joinInfo.mid,
                data : 'mid='+joinInfo.mid+'&token='+joinInfo.token
            };

            window.getData(opt,function(data){
                var footer = data.room.order.footer;
                var left = data.room.order.left;
                var right = data.room.order.right;
                var footer_data = data.room.players[footer];
                var left_data = data.room.players[left];
                var right_data = data.room.players[right];
                $('.footerImg').attr('src',footer_data.wxavatar);
                $('.footerName').html(footer_data.wxname);
                $('.footerNumber').html(footer_data.point);
                $('.leftImg').attr('src',left_data.wxavatar);
                $('.leftName').html(left_data.wxname);
                $('.leftNumber').html(left_data.point);
                $('.rightImg').attr('src',right_data.wxavatar);
                $('.rightName').html(right_data.wxname);
                $('.rightNumber').html(right_data.point);
                $('.roomInfo').html(data.room.room.pwd);
            });
        },
        Hupais : function(evtData,arr,_self){

            var p = arr;
            var t = '';
            for(var i=0;i< p.length;i++){
                t+='.'+p[i].cate
            };
            t+=evtData.cid.split('-')[1];
            var str = '';
            var arrt = t.split('.');
            var sortarr = arrt.sort();
            for(var i=0;i<sortarr.length;i++){
                if(sortarr[i]!=""){
                    str+=sortarr[i];
                }
            }
            var isHu = _self.strHp(str,_self);

            if(isHu=='Max'){
                $('#blockHupai').css('display','block');
                $('#blockpeng').css('display','none');
                $('#blockgang').css('display','none');
                $('#blockliang').css('display','none');
                $('#blockHupai').on('click',function(){
                    var urlData = w.location.href;
                    var rid = urlData.split('/')[urlData.split('/').length-4];
                    var mid = urlData.split('/')[urlData.split('/').length-3];
                    var pasId = urlData.split('/')[urlData.split('/').length-5];
                    urlData = ['tels',urlData.split('/')[urlData.split('/').length-5]];
                    if(_self.doms.zimo==1){
                        var str = {
                            type : 8,
                            rid : rid,
                            role : pasId,
                            hu : '1',
                            zimo : 1
                        };
                    }else {
                        var str = {
                            type: 8,
                            rid: rid,
                            role: pasId,
                            hu: '1',
                        };
                    }
                    str = JSON.stringify(str);
                    ws.send(str);
                    var val = _self.getStorage(_self);
                    _self.oveTemplat(val);
                    $('#over').css('display','block');
                    var over = {
                        type : 10,
                        rid : rid
                    };
                    over = JSON.stringify(over);
                    $('.startGame').on('click',function(){
                        ws.send(over);
                    });
                });
            }else if(isHu!=0){
                $('#blockHupai').css('display','block');
                $('#blockpeng').css('display','none');
                $('#blockgang').css('display','none');
                $('#blockliang').css('display','none');
            }
        },
        peng : function(evtData,val,urlData,mid,next,rid,_slef){
            var status = _slef.checkstatus(evtData,val,urlData,mid,next,rid);
            var roles = {
                host : ['r2','host','r1'],
                r1 : ['host','r1','r2'],
                r2 : ['r1','r2','host']
            };
            var data = evtData.cid.split('-')[1];
            var cid = '';
            var id = '';
            var pre = roles[evtData.next][0];
            for(var i=0;i<val.length;i++){
                if(val[i].cate==data){

                    cid = val[i].type+val[i].cate;
                    id = val[i].id;
                }
            }
            if(status==2){
                if(_slef.doms.xiaohubunengliang!=1) {
                    _slef.doms.blockgang.css('display', 'block');
                    _slef.doms.blockgang.find('img').one('click', function () {
                        if ($(this).index() == 0) {
                            var options = {
                                type: 7,
                                rid: rid,
                                mid: mid,
                                cid: cid,
                                id: evtData.id,
                                next: urlData,
                                status: 3,
                                role: urlData
                            };
                            options = JSON.stringify(options);
                            ws.send(options);
                            _slef.doms.blockgang.css('display', 'none');

                            _slef.deletePeng(val, evtData, _slef, urlData);

                        }
                        if ($(this).index() == 1) {
                            var options = {
                                type: 7,
                                rid: rid,
                                mid: mid,
                                cid: cid,
                                id: evtData.id,
                                next: urlData,
                                status: 2,
                                role: urlData,
                                pre: pre
                            };
                            options = JSON.stringify(options);
                            ws.send(options);
                            _slef.doms.audio.attr('src','../audio/gang.m4a');
                            _slef.doms.blockgang.css('display', 'none');
                            _slef.deletePeng(val, evtData, _slef, urlData, 'gang');
                        }
                        if ($(this).index() == 2) {
                            var options = {
                                type: 7,
                                rid: rid,
                                mid: mid,
                                cid: cid,
                                id: evtData.id,
                                next: evtData.next,
                                status: 0,
                                role: urlData,
                                pre: pre
                            };
                            options = JSON.stringify(options);
                            ws.send(options);
                            _slef.doms.blockgang.css('display', 'none');
                        }
                        return

                    })
                }
            }else
            if(status==3) {
                if(_slef.doms.xiaohubunengliang!=1) {
                    if ($('#blockHupai').css('display') != 'block') {
                        _slef.doms.blockpeng.css('display', 'block');
                    }
                    _slef.doms.blockpeng.find('img').one('click', function () {
                        if ($(this).index() == 0) {
                            var options = {
                                type: 7,
                                rid: rid,
                                mid: mid,
                                cid: cid,
                                id: evtData.id,
                                next: urlData,
                                status: 3,
                                role: urlData,
                                pre: pre
                            };
                            options = JSON.stringify(options);
                            ws.send(options);
                            _slef.doms.audio.attr('src','../audio/peng.m4a');
                            _slef.doms.blockpeng.css('display', 'none');
                            _slef.deletePeng(val, evtData, _slef, urlData);
                        }
                        if ($(this).index() == 1) {
                            var options = {
                                type: 7,
                                rid: rid,
                                mid: mid,
                                cid: cid,
                                id: evtData.id,
                                next: evtData.next,
                                status: 0,
                                role: urlData,
                                pre: pre
                            };
                            options = JSON.stringify(options);
                            ws.send(options);
                            _slef.doms.blockpeng.css('display', 'none');
                        }
                        return
                    });
                }
            }else{
                var options = {
                    type: 7,
                    rid: rid,
                    mid: mid,
                    cid: cid,
                    id: evtData.id,
                    next: evtData.next,
                    status: status,
                    role: urlData,
                    pre :pre
                };

                options = JSON.stringify(options);
                ws.send(options);
            }
        },
        deletePeng : function(data,val,_self,urldata,gang){
            var deletedata = new Array();
            var val = val.cid.split('-')[1];
            for(var i=0;i<data.length;i++){
                if(data[i].cate==val){
                    deletedata.push(data.splice(i,1));
                    break
                }
            }
            for(var i=0;i<data.length;i++){
                if(data[i].cate==val){
                    deletedata.push(data.splice(i,1));
                    break
                }
            }
            if(gang){
                for(var i=0;i<data.length;i++){
                    if(data[i].cate==val){
                        deletedata.push(data.splice(i,1));
                        break
                    }
                }
            }
            _self.pengTemplate(data,_self,urldata);
            _self.deletepengtemplate(deletedata,_self)

        },
        deletepengtemplate : function(deletedata,_self){
            _self.doms.mypengPai.css('display','block');
            var getName = _self.getHoom() ;
            _self.setcookie(getName);
            if(deletedata.length==3){
                var text = '';
                var datagrid = deletedata;
                text += '<li dataIndex=' + datagrid[0][0].id + ' data="' + datagrid[0][0].type + '-' + datagrid[0][0].cate + '">' +
                    '<img src="../imgs/' + datagrid[0][0].type + '-' + datagrid[0][0].cate + '_center_gang.png" alt=""/>' +
                    '</li>'
            }else {
                var text = '';
                var datagrid = deletedata;
                text += '<li dataIndex=' + datagrid[0][0].id + ' data="' + datagrid[0][0].type + '-' + datagrid[0][0].cate + '">' +
                    '<img src="../imgs/' + datagrid[0][0].type + '-' + datagrid[0][0].cate + '_center_peng.png" alt=""/>' +
                    '</li>'
            }
            _self.doms.mypengPai.append(text);
        },
        checkstatus : function(evtData,val,urlData,mid,next,rid,_slef){
            var data = evtData.cid.split('-')[1];
            var test = '';
            for(var i=0;i<val.length;i++){
                test += val[i].cate+'-'
            }
            var testarr = test.split('-');
            testarr.sort();
            test = '';
            for(var i=0;i<testarr.length;i++){
                if(testarr[i]!=''){
                    test += testarr[i]
                }
            }
            if(test.indexOf(data+data+data)!=-1){
                return 2
            }else
            if(test.indexOf(data+data)!=-1){
                return 3
            }

            return 0
        },
        overpais : function(obj,cid,r){

            if(cid.indexOf('-')!=-1) {
//           var text = $('.leftoverpais').html();
                var text = '';
                text = '<li>' +
                    '<img src="../imgs/' + cid + '_'+ r +'.png" alt=""/>' +
                    '</li>';
                obj.append(text);
            }
        },
        strHp : function(str,_self){
            for(var i=0;i<hupaizhengzhe.length;i++){
                var h = hupaizhengzhe[i].preg.test(str);
                if(h){
                    return hupaizhengzhe[i].bigorsmall
                }
            }
            var pai = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u'];
            for(j in pai){
                var index = 0;
                for(var i=0;i<str.length;i++){
                    if(str[i]==pai[j]){
                        index++;
                    }
                }
                pai[j]=index;
            }
            var cards =[
                0,
                pai[0],pai[1],pai[2],pai[3],pai[4],pai[5],pai[6],pai[7],pai[8],         //   PAI[1-9]     壹条～玖条的个数
                0,
                pai[9],pai[10],pai[11],pai[12],pai[13],pai[14],pai[15],pai[16],pai[17],         //   PAI[11-19]   壹铜～玖铜的个数
                0,
                pai[18],pai[19],pai[20]                     //  PAI[21-23]    中发白的个数
            ];
            var arr = [[],[],[],[]]
            var isHu = hu(cards,arr);
            var isHu1 = hu1(cards,arr);
            var isHu2 = hu2(cards,arr);
            if(isHu==0&&isHu1==0&&isHu2==0){
                return 0
            }else{
                return 1
            }

        },
        Hupai : function(val,arr,urlData,next,_self){   //胡牌自摸
            var p = arr[urlData[1]];
            p.push(val);
            var t = '';
            for(var i=0;i< p.length;i++){
                t+='.'+p[i].cate
            };
            var str = '';
            var arrt = t.split('.');
            var sortarr = arrt.sort();
            for(var i=0;i<sortarr.length;i++){
                if(sortarr[i]!=""){
                    str+=sortarr[i];
                }
            }
            var isHu = _self.strHp(str,_self);
            if(urlData[1]==next){
                if(isHu){
                    _self.doms.zimo = 1
                    $('#blockHupai').css('display','block');
                    $('#blockpeng').css('display','none');
                    $('#blockgang').css('display','none');
                    $('#blockliang').css('display','none');
                };
            }
        },
        oveTemplat : function(val){
            var host = val[2].data[0].playlist.host;
            var r1 = val[2].data[0].playlist.r1;
            var r2 = val[2].data[0].playlist.r2;
            $('.oneImg').html('');
            $('.toImg').html('');
            $('.sImg').html('');
            var text1 = '';
            for(i in host){
                text1+='<img src="../imgs/'+host[i].type+'-'+host[i].cate+'.png" />'
            }
            var text2 = '';
            for(i in r1){
                text2+='<img src="../imgs/'+r1[i].type+'-'+r1[i].cate+'.png" />'
            }
            var text3 = '';
            for(i in r2){
                text3+='<img src="../imgs/'+r2[i].type+'-'+r2[i].cate+'.png" />'
            }
            $('.oneImg').append(text1);
            $('.toImg').append(text2);
            $('.sImg').append(text3);
        },
        addEvent : function(){
            var _self = this;
            $('.setting').on('click',function(){
                $('#bgyingyue').attr('src','')
            })
            _self.doms.blockliang.find('img').on('click',function(){
                if($(this).index()==0){
                    _self.doms.xiaohubunengliang=1;
                    $(this).parent().css('display','none');
                    _self.doms.dianjizhezao.css('display','block');
                    $('.MyPai').find('li>img').css('opacity','0.7');
                    _self.doms.liangNumber = 2;
                    var urlData = w.location.href;
                    var rid = urlData.split('/')[urlData.split('/').length-4];
                    var pasId = urlData.split('/')[urlData.split('/').length-5];
                    var options = {
                        rid : rid,
                        role : pasId,
                        type : 9
                    };
                    options = JSON.stringify(options);
                    ws.send(options);
                }else{
                    $(this).parent().css('display','none');
                    var options = {
                        rid : rid,
                        role : pasId,
                        type : 9
                    };
                }
            });
            _self.doms.MyPais.delegate('li','click',function(){   //摸进来的牌
                var s = this;  //点击的this

                if($(this).css('top')=='-20px'){
                    _self.chupai($(this));
                    setTimeout(function(){
                        _self.removePai(s,_self);
                        _self.setWidth(_self.doms.width);
                    },700);
                }else{
                    _self.doms.MyPai.find('li').animate({
                        marginTop : 0
                    });
                    _self.MyTop($(this),1);
                }
            });
            $('#blockHupai').on('click',function(){
                if(_self.doms.xiaohubunengliang==1) {
                    var urlData = w.location.href;
                    var rid = urlData.split('/')[urlData.split('/').length - 4];
                    var mid = urlData.split('/')[urlData.split('/').length - 3];
                    var pasId = urlData.split('/')[urlData.split('/').length - 5];
                    urlData = ['tels', urlData.split('/')[urlData.split('/').length - 5]];
                    var textHtml = new Array();;
                    for(var i=0;i<$('.MyPai').find('li').length;i++){
                        textHtml.push($('.MyPai').find('li').eq(i).attr('dataindex'));
                    }

                    var str = {
                        type: 8,
                        rid: rid,
                        role: pasId,
                        hu: '1',
                        cards : textHtml
                    };
                    str = JSON.stringify(str);
                    ws.send(str);
                    var val = _self.getStorage(_self);
                    _self.oveTemplat(val);
                    $('#over').css('display', 'block');
                    var over = {
                        type: 10,
                        rid: rid
                    };
                    over = JSON.stringify(over);
                    $('.startGame').on('click', function () {
                        ws.send(over);
                    });
                }else{
                    alert('小胡要先亮牌');
                    $('#blockHupai').css('display','none');
                }
            });
            _self.doms.MyPai.delegate('li','click',function(){  //本来的牌
                var s = this;  //点击的this
                if(_self.doms.xianzhichupai) {
                    if (_self.doms.mydata.css('display') == 'block') {
                        if ($(this).css('marginTop') == '-20px') {
                            _self.chupai($(this));
                            setTimeout(function () {
                                _self.removePai(s, _self, true);
                                _self.setWidth(_self.doms.width);
                            }, 700);

                        } else {
                            _self.doms.MyPais.find('li').animate({
                                top: 0
                            });
                            $(this).siblings().animate({
                                marginTop: 0
                            });
                            _self.MyTop($(this));
                        }
                    }
                }
            })
        },
        displayCp : function(urlData,data,s,k,j){   //判断谁出牌
            if(k){
                if(j){
                    var index =  urlData.indexOf(data);
                }else{
                    var index = urlData.indexOf(data.next);
                }
                for (var i = 0; i < $('.dataId').length; i++) {
                    if ($('.dataId').eq(i).attr('tt') == index) {
                        $('.dataId').eq(i).css('display', 'block')
                    } else {
                        $('.dataId').eq(i).css('display', 'none')
                    }
                }
            }else {

                var zhuangjia = JSON.parse(data);
                var index = urlData.indexOf(zhuangjia.playlist.banker);
                for (var i = 0; i < $('.dataId').length; i++) {
                    if ($('.dataId').eq(i).attr('tt') == index) {
                        $('.dataId').eq(i).css('display', 'block')
                    } else {
                        $('.dataId').eq(i).css('display', 'none')
                    }
                }
            }
        },
        getHoom : function(){
            var urlData = w.location.href;
            var urlData = w.location.href;
            var rid = urlData.split('/')[urlData.split('/').length-4];
            var mid = urlData.split('/')[urlData.split('/').length-3];
            var pasId = urlData.split('/')[urlData.split('/').length-5];
            urlData = ['tels',urlData.split('/')[urlData.split('/').length-5]];
            return pasId+rid+mid

        },//获取房间号用户Id
        templateMy : function(_self,data,urlData,t,callback,removeSrot){  //  My的模板
            _self.doms.MyPai.html('');
            var data = JSON.parse(data);
            var datagrid = data.playlist[urlData];
            if(removeSrot!=undefined){
                var val = {
                    cate : $('.MyPais').find('li').attr('cate'),
                    id : $('.MyPais').find('li').attr('dataindex'),
                    name : $('.MyPais').find('li').attr('name'),
                    type : $('.MyPais').find('li').attr('type'),
                    val : $('.MyPais').find('li').attr('val')
                };
                if(val.cate!=undefined){
                    if(datagrid.length<13){
                        datagrid.push(val);
                    }
                }
            }
            var dataList = _self.getStorage(_self);
            dataList[2].data[0].playlist[urlData]= datagrid;
            var name = _self.getHoom();
            _self.getcookie(dataList,name);
            var text = '';
            data = _self.srot(data,urlData);
            if(data.playlist.banker==urlData) {
                if (t) {
                    for (var i = 0; i < datagrid.length; i++) {
                        text += '<li dataIndex=' + datagrid[i].id + ' data="' + datagrid[i].type + '-' + datagrid[i].cate + '">' +
                            '<img src="../imgs/' + datagrid[i].type + '-' + datagrid[i].cate + '.png" alt=""/>' +
                            '</li>'
                    }
                    callback && callback();
                } else {
                    for (var i = 0; i < datagrid.length - 1; i++) {
                        text += '<li dataIndex=' + datagrid[i].id + ' data="' + datagrid[i].type + '-' + datagrid[i].cate + '">' +
                            '<img src="../imgs/' + datagrid[i].type + '-' + datagrid[i].cate + '.png" alt=""/>' +
                            '</li>'
                    }
                    _self.templateMyOne(_self, datagrid[datagrid.length - 1], urlData[1]);
                }
            }else{
                if (t) {
                    for (var i = 0; i < datagrid.length; i++) {
                        text += '<li dataIndex=' + datagrid[i].id + ' data="' + datagrid[i].type + '-' + datagrid[i].cate + '">' +
                            '<img src="../imgs/' + datagrid[i].type + '-' + datagrid[i].cate + '.png" alt=""/>' +
                            '</li>'
                    }
                    callback && callback();
                } else {
                    for (var i = 0; i < datagrid.length; i++) {
                        text += '<li dataIndex=' + datagrid[i].id + ' data="' + datagrid[i].type + '-' + datagrid[i].cate + '">' +
                            '<img src="../imgs/' + datagrid[i].type + '-' + datagrid[i].cate + '.png" alt=""/>' +
                            '</li>'
                    }

                    $('.MyPais').find('li').remove();
                }
            }
            _self.doms.MyPai.append(text);
        },
        templateMyOne : function(_self,data,urlData){  //  摸进来的牌
            _self.doms.MyPais.html('');
            var text = '';
            text += '<li type='+data.type+' val='+data.val+' dataindex='+data.id+' cate='+data.cate+'  name='+data.name+'  data="'+data.type+'-'+data.cate+'">' +
                '<img src="../imgs/'+data.type+'-'+data.cate+'.png" alt=""/>' +
                '</li>'
            _self.doms.MyPais.append(text);
        },
        pengTemplate : function(data,_self,urlData){
            //碰牌的模板
            _self.doms.MyPai.html('');
            _self.srot(data,'',true);
            var datagrid = data;
            var text = '';
            for (var i = 0; i < datagrid.length; i++) {
                text += '<li dataIndex=' + datagrid[i].id + ' data="' + datagrid[i].type + '-' + datagrid[i].cate + '">' +
                    '<img src="../imgs/' + datagrid[i].type + '-' + datagrid[i].cate + '.png" alt=""/>' +
                    '</li>'
            }
            _self.doms.MyPai.append(text);
            var dataList = _self.getStorage(_self);
            dataList[2].data[0].playlist[urlData]= datagrid;
            var name = _self.getHoom();
            _self.getcookie(dataList,name);
            _self.doms.width += 0.22;
            _self.setWidth(_self.doms.width);
        },
        templateLeft : function(_self,data,urlData,peng,gang){  //左边的牌模板
            if(peng){
                if(gang){
                    _self.doms.leftPais.find('li').eq(0).remove();
                    _self.doms.leftPais.find('li').eq(1).remove();
                    _self.doms.leftPais.find('li').eq(1).remove();
                    var datagrid = new Array();
                    data = data.cid.replace(data.cid.substring(data.cid.length - 1), '-' + data.cid.substring(data.cid.length - 1));
                    datagrid.push(data);
                    var text = '';
                    text += '<li class="leftPai dao">' +
                        '<img src="../imgs/' + datagrid[0] + '_left_gang.png" alt=""/>' +
                        '</li>'
                    _self.doms.leftPais.append(text);
                }else {
                    _self.doms.leftPais.find('li').eq(0).remove();
                    _self.doms.leftPais.find('li').eq(1).remove();
                    _self.doms.leftPais.find('li').eq(1).remove();
                    var datagrid = new Array();
                    data = data.cid.replace(data.cid.substring(data.cid.length - 1), '-' + data.cid.substring(data.cid.length - 1));
                    datagrid.push(data);
                    var text = '';
                    text += '<li class="leftPai dao">' +
                        '<img src="../imgs/' + datagrid[0] + '_left_peng.png" alt=""/>' +
                        '</li>'
                    _self.doms.leftPais.append(text);
                }
            }else {
                _self.doms.leftPais.html('');
                var data = JSON.parse(data);
                var datagirid = data.playlist[urlData];
                var text = '';
                for (var i = 0; i < datagirid.length; i++) {
                    text += '<li class="leftPai">' +
                        '<img src="../imgs/pai_left.png" alt=""/>' +
                        '</li>'
                }
                _self.doms.leftPais.append(text);
            }
        },
        templateRight : function(_self,data,urlData,peng,gang){ //右边的牌
            if(peng){
                if(gang){
                    _self.doms.RightPais.find('li').eq(0).remove();
                    _self.doms.RightPais.find('li').eq(1).remove();
                    _self.doms.RightPais.find('li').eq(1).remove();
                    var datagrid = new Array();
                    data = data.cid.replace(data.cid.substring(data.cid.length - 1), '-' + data.cid.substring(data.cid.length - 1));
                    datagrid.push(data);
                    var text = '';
                    text += '<li class="RightPai dao">' +
                        '<img src="../imgs/' + datagrid[0] + '_right_gang.png" alt=""/>' +
                        '</li>';
                    _self.doms.RightPais.append(text);
                }else {
                    _self.doms.RightPais.find('li').eq(0).remove();
                    _self.doms.RightPais.find('li').eq(1).remove();
                    _self.doms.RightPais.find('li').eq(0).remove();
                    var datagrid = new Array();
                    data = data.cid.replace(data.cid.substring(data.cid.length - 1), '-' + data.cid.substring(data.cid.length - 1));
                    datagrid.push(data);
                    var text = '';
                    text += '<li class="RightPai dao">' +
                        '<img src="../imgs/' + datagrid[0] + '_right_peng.png" alt=""/>' +
                        '</li>'
                    _self.doms.RightPais.append(text);
                }
            }else {
                _self.doms.RightPais.html('');
                var data = JSON.parse(data);
                var datagirid = data.playlist[urlData];
                var text = '';
                for (var i = 0; i < datagirid.length; i++) {
                    text += '<li class="RightPai">' +
                        '<img src="../imgs/pai_Right.png" alt=""/>' +
                        '</li>'
                }
                _self.doms.RightPais.append(text);
            }
        },
        time : function(){   //倒计时
            var _self = this;
            var initTime = _self.doms.time.html();
            var clearTime = setInterval(function(){
                initTime--;
                if(initTime<10){
                    _self.doms.time.css('marginLeft','-5.5px')
                }
                if(initTime<0){
                    clearInterval(clearTime);
                }else{
                    _self.doms.time.html(initTime);
                }
            },1000)
        },
        MyTop : function(obj,v){   //点击第1下的时候移出去
            if(v){
                obj.animate({
                    top: '-20px'
                },100)
            }else {
                obj.animate({
                    marginTop: '-20px'
                }, 100)
            }
        },
        chupai : function(obj){   //点击第二下的时候移出去
            obj.animate({
                marginTop : '-100px',
                opacity : '0'
            });
        },
        removePai : function(obj,_self,t){ //点击第二下的时候删除
            _self.storage(obj,_self);
            obj.remove();
            var chupaisehngyi = $(obj).attr('data');
            var storageVal = _self.getStorage(_self);
            _self.palySize(storageVal[0].jsonList,_self);
            _self.doms.audio.attr('src','../audio/'+chupaisehngyi+'.m4a');
            var urlData = w.location.href;
            var rid = urlData.split('/')[urlData.split('/').length-4];
            var mid = urlData.split('/')[urlData.split('/').length-3];
            var pasId = urlData.split('/')[urlData.split('/').length-5];
            urlData = ['tels',urlData.split('/')[urlData.split('/').length-5]];
            var ts = urlData;
            var roles = {
                host: ['r2', 'host', 'r1'],
                r1: ['host', 'r1', 'r2'],
                r2: ['r1', 'r2', 'host']
            };

            var cp = roles[urlData[1]][2];
            var urlData = roles[urlData[1]];
            var removeIndex = $(obj).attr('dataindex');
            var dataList = _self.getStorage(_self);
            var arrList = dataList[2].data[0].playlist[ts[1]];
            for(var i=0;i<arrList.length;i++){
                if(arrList[i].id==removeIndex){
                    arrList.splice(i,1)
                }
            };
            dataList[2].data[0].playlist[ts[1]]=arrList;
            dataList = dataList[2].data[0];
            dataList = JSON.stringify(dataList);
            if(t){
                _self.templateMy(_self,dataList,urlData[1],true,function(){
                    $('.MyPais').find('li').remove();
                },'removeSrot')

            }
            var liang = _self.liangpai(arrList,_self);
            _self.doms.xianzhichupai = false;

//            if(liang.length>0){
//                _self.doms.blockliang.on('click',function(){
//                    _self.doms.blockliang.css('display','none');
//                    _self.displayCp(urlData,cp,_self,true,true);
//                })
//            }else{
//                _self.displayCp(urlData,cp,_self,true,true);
//            }

        },
        liangpai : function(data,_slef){
            var pai = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u'];
            var text = '';
            var queArrpai = new Array();
            for( i in data){
                text+=data[i].cate
            }
            for(i in pai){
                text+= pai[i];
                var isJson = _slef.strHp(text);
                text = text.substring(0,text.length-1);
                if(isJson!=0){
                    queArrpai.push( pai[i]);
                    console.log(isJson)
                        if(_slef.doms.liangNumber==1){
                            _slef.doms.blockliang.css('display', 'block');
                        }
                }
            }
            return  queArrpai
        },
        Shuffle : function(){  //打完牌后的数据清洗
            var _self = this;
            _self.templateMy();
        },
        ponstSend : function(obj){   //给家昌发送数据
            ws.send(obj);
        },
        setWidth : function(width){
            var _self = this;
            _self.doms.MyPai.css('marginLeft','-'+_self.doms.MyPai.find('li').length*6/width+'%')
        },
        storage : function(obj,_self){   //储牌已经打出的牌
            var storageVal = _self.getStorage(_self);
            storageVal[0].jsonList.push($(obj).attr('data'));
            var urlData = w.location.href;
            var rid = urlData.split('/')[urlData.split('/').length-4];
            var mid = urlData.split('/')[urlData.split('/').length-3];
            var pasId = urlData.split('/')[urlData.split('/').length-5];
            urlData = ['tels',urlData.split('/')[urlData.split('/').length-5]];

            var roles = {
                host : ['r2','host','r1'],
                r1 : ['host','r1','r2'],
                r2 : ['r1','r2','host']
            };
            urlData =  roles[urlData[1]];
            var jsonId = {
                role :pasId,
                rid : rid,
                mid : mid,
                cid : $(obj).attr('data'),
                id :  $(obj).attr('dataindex'),
                type : 5,
                next : urlData[urlData.length-1]
            };
            jsonId = JSON.stringify(jsonId);
            _self.ponstSend(jsonId);  //给家昌发送打出去的牌
            var key = _self.getHoom();
            _self.getcookie(storageVal, key);
        },
        getStorage : function(_self){  //读取本地数据
            var key = _self.getHoom();
            var jsonList = localStorage.getItem(key);
            var arr = JSON.parse(jsonList);
            return arr
        },
        getcookie : function(val,name){
            localStorage.setItem(name,JSON.stringify(val))
        },
        setcookie : function(name){
            var jsonList = localStorage.getItem(name);
            jsonList = JSON.parse(jsonList);
            return jsonList
        },
        palySize : function(val,_self){
            //掉线处理的时候要麻烦点
            var text = '';
            text += '<li>' +
                '<img src="../imgs/'+val[val.length-1]+'_center.png" alt=""/>' +
                '</li>'
            _self.doms.mplay.append(text);
        },
        clearStorage : function(obj){ //清空本地存储
            localStorage.setItem(obj,'undefined');
        },
        srot : function(arr,urlData,peng){ //排序
            if(peng){
                var datagrid = arr;
            }else{
                var datagrid = arr.playlist[urlData];
            }

            for(var i=0;i<datagrid.length;i++){
                for(var j=i+1;j<datagrid.length;j++){
                    if(datagrid[i].type>datagrid[j].type){
                        datagrid[j] = datagrid.splice(i,1,datagrid[j])[0];
                    }
                }
            }
            for(var i=0;i<datagrid.length;i++){
                for(var j=i+1;j<datagrid.length;j++){
                    if(datagrid[i].type == datagrid[j].type){
                        if(datagrid[i].val >datagrid[j].val){
                            datagrid[j] = datagrid.splice(i,1,datagrid[j])[0];
                        }
                    }
                }
            }
            return arr;
        }
    };
    opt.init();

})(window);